package com.travel.Controller;

import com.github.pagehelper.PageInfo;
import com.travel.Bean.Culture;
import com.travel.Dao.CultureMapper;
import com.travel.JPA.CultureJPA;
import com.travel.Service.Impl.CultureServiceImpl;
import com.travel.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Api("景区管理页面控制类，Super")
@RestController()
@RequestMapping("/SuperCulture")
public class SuperCultureController {
    @Autowired
    private CultureServiceImpl cultureService;


    //获取管理员权限信息
    public String getAdminRole() {
        String role;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            role = ((UserDetails) principal).getAuthorities().toString();
        } else {
            role = principal.toString();
        }
        return role;
    }


    //获取管理员名称
    public String getUsername() {
        String username;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        return username;
    }


    @ApiOperation("景区列表页面")
    @GetMapping("/CulturePage")
    public ModelAndView CulturePage(){
        return new ModelAndView("super/CulturePage");

    }


    /**
     * 景区列表
     *
     * @param cultureName 景区名字
     * @param page        页面
     * @param limit       记录条数
     * @return {@link R}
     */
    @ApiOperation("获取景区列表信息")
    @PostMapping("CultureList")
    public R CultureList(String cultureName, @RequestParam(defaultValue = "1")Integer page,
                         @RequestParam(defaultValue = "15") Integer limit){
        PageInfo<Culture> pageInfo = cultureService.selCultureList(cultureName,page,limit);

        return R.ok("0",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 景区编辑页面
     *
     * @param id id
     * @return {@link ModelAndView}
     */
    @ApiOperation("景区信息修改页面")
    @GetMapping("/CultureEditPage")
    public ModelAndView CultureEditPage(Integer id){
        ModelAndView modelAndView = new ModelAndView("public/CultureEdit");
        Culture culture = cultureService.findCultureById(id);
        modelAndView.addObject("type","culture");
        System.out.println(culture);
        modelAndView.addObject("culture",culture);
        return modelAndView;
    }

    /**
     * 景区添加页面
     *
     * @return {@link ModelAndView}
     */
    @ApiOperation("添加景区页面")
    @GetMapping("/CultureAddPage")
    public ModelAndView CultureAddPage(){
        return new ModelAndView("public/CultureAdd");
    }


    /**
     * 保存景区编辑
     *
     * @param culture 景区
     * @return {@link R}
     */
    @ApiOperation("保存景区修改的信息")
    @PostMapping("saveCultureEdit")
    public R saveCultureEdit(Culture culture){

        return R.ok(cultureService.updateCultureEdit(culture));
    }


    /**
     * 保存景区添加
     *
     * @param culture 景区
     * @return {@link R}
     */
    @ApiOperation("保存添加的景区信息")
    @PostMapping("saveCultureAdd")
    public R saveCultureAdd(Culture culture){
        String username = getUsername();
        return R.ok(cultureService.saveCultureAdd(culture,username));
    }


    /**
     * 删除景区
     *
     *
     * @param id id
     * @return {@link R}
     */
    @Transactional
    @ApiOperation("景区删除")
    @PostMapping("/DelCulture")
    public R DelCulture(Integer id){
        if(cultureService.deleteCulture(id)>0){
            return R.ok(0);
        }
        return R.no(1);
    }


}
